// 学校首页
homeApp.controller("schoolIndexCtr",function($scope,userService,commonService,schoolService){
	$(".start_loading").addClass("loading");

	$(window).resize(function(){
		location.reload();
	})

	$scope.showSearch = false;
	$scope.searchTitle = "展开搜索";

	var curPage = 1;
	var curDep = 0;
	var schoolId = 0;

	if(userService.userIsLogin()){
		$scope.isLogin = true;
	}else{
		$scope.isLogin = false;
	}

	if($(window).width() >= 1000){
		$scope.showSchoolDetail = true;
	}else{
		$scope.showSchoolDetail = false;
	}

	commonService.httpReq(schoolListUrl,"",function(response){
		if(response.STATUS == SUCCESS){
			var res = response.RESULT;
			schoolId = res.school["0"].id;
			$scope.schooleList = res.school;

			var formDataStr = $.param({
				p: curPage,
				pageSize: 20,
				school: res.school["0"].id
			})

			if($scope.showSchoolDetail){
				commonService.httpReq(getTeacherBySchool,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var res = response.RESULT;
						if(res.count > 20){
							$scope.showMe = true;
						}else{
							$scope.showMe = false;
						}
						$.each(res.list,function(index,value){
							value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
							value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
							value.description = (value.description=="")?"懒得留描述":value.description;
							value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
							value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
						})
						$scope.list = res.list;
						$scope.totalPage = parseInt(res.count/20)+1;
						$scope.curPage = parseInt(curPage);
						$scope.pageList = commonService.totalPageList(parseInt(curPage),parseInt($scope.totalPage));
						$(".start_loading").removeClass("loading");
					}
				})
			}else{
				$(".start_loading").removeClass("loading");
			}
		}
	})

	$scope.getTeacherBySchool = function($event,id){
		curPage = 1;

		schoolService.removeParentClass($event,'active');
		schoolService.addParentClass($event,'active');

		var formDataStr = $.param({
			p: curPage,
			pageSize: 20,
			school: id,
			did: 0
		})

		commonService.httpReq(getTeacherBySchool,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				var res = response.RESULT;
				if(res.count > 20){
					$scope.showMe = true;
				}else{
					$scope.showMe = false;
				}
				$.each(res.list,function(index,value){
					value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
					value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
					value.description = (value.description=="")?"懒得留描述":value.description;
					value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
					value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
				})
				$scope.list = res.list;
				$scope.totalPage = parseInt(res.count/20)+1;
				$scope.curPage = parseInt(curPage);
				$scope.pageList = commonService.totalPageList(parseInt(curPage),parseInt($scope.totalPage));
			}
		})
	}

	$scope.getTeacherByDepartment = function($event,id){
		curPage = 1;
		curDep = id;
		schoolService.removeSiblingsClass($event,'current');
		schoolService.addSelfClass($event,'current');

		var formDataStr = $.param({
			p: curPage,
			pageSize: 20,
			school: schoolId,
			did: id
		})

		commonService.httpReq(getTeacherByDepartment,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				var res = response.RESULT;
				if(res.count > 20){
					$scope.showMe = true;
				}else{
					$scope.showMe = false;
				}
				$.each(res.list,function(index,value){
					value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
					value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
					value.description = (value.description=="")?"懒得留描述":value.description;
					value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
					value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
				})
				$scope.list = res.list;
				$scope.totalPage = parseInt(res.count/20)+1;
				$scope.curPage = parseInt(curPage);
				$scope.pageList = commonService.totalPageList(parseInt(curPage),parseInt($scope.totalPage));
			}
		})
	}

	$scope.page = function(num){
        $(".start_loading").addClass("loading");
		curPage = num;

		var formDataStr = $.param({
			p: curPage,
			pageSize: 20,
			school: schoolId,
			did: curDep,
		});

		commonService.httpReq(getTeacherBySchool,formDataStr,function(response){

			if(response.STATUS == SUCCESS){
				var res = response.RESULT;
				$.each(res.list,function(index,value){
					value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
					value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
					value.description = (value.description=="")?"懒得留描述":value.description;
					value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
					value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
				})
				$scope.list = res.list;
				$scope.totalPage = parseInt(res.count/20)+1;
				$scope.curPage = parseInt(curPage);
				$scope.pageList = commonService.totalPageList(parseInt(curPage),parseInt($scope.totalPage));
				$(".start_loading").removeClass("loading");
			}

		})

	}
})

// 详情
homeApp.controller("schoolDetailCtr",function($scope,$routeParams,userService,commonService){
	$(".start_loading").addClass("loading");
	var id = $routeParams.id;
	if(userService.userIsLogin()){
		$scope.isLogin = true;
	}else{
		$scope.isLogin = false;
	}

	$scope.dep = '0';

	var curPage = 1;
	var curDep = 0;
	var count = 0;
	var formDataStr = $.param({
		p: curPage,
		pageSize: 10,
		school: id
	})

	commonService.httpReq(getTeacherBySchool,formDataStr,function(response){
		if(response.STATUS == SUCCESS){
			var res = response.RESULT;
			$scope.title = res.list[0].school_name;
			$.each(res.list,function(index,value){
				value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
				value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
				value.description = (value.description=="")?"懒得留描述":value.description;
				value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
				value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
			})
			$scope.list = res.list;
			$scope.department = res.department;
			count = res.count;
			count = res.count;
			if(count > 10){
				$scope.showMe = true;
			}else {
				$scope.showMe = false;
			}
			$(".start_loading").removeClass("loading");
		}
	})

	$scope.getTeachersByDepartment = function(){
		curPage = 1;

		if($scope.dep == 0){
			var formDataStr = $.param({
				p: curPage,
				pageSize: 20,
				school: id,
				did: 0
			})

			commonService.httpReq(getTeacherBySchool,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					var res = response.RESULT;
					$.each(res.list,function(index,value){
						value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
						value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
						value.description = (value.description=="")?"懒得留描述":value.description;
						value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
						value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
					})
					$scope.list = res.list;
					count = res.count;
					if(count > 10){
						$scope.showMe = true;
					}else {
						$scope.showMe = false;
					}
				}
			})
		}else{
			var formDataStr = $.param({
				p: curPage,
				pageSize: 20,
				school: id,
				did: $scope.dep
			})

			commonService.httpReq(getTeacherByDepartment,formDataStr,function(response){
				if(response.STATUS == SUCCESS){
					var res = response.RESULT;
					$.each(res.list,function(index,value){
						value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
						value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
						value.description = (value.description=="")?"懒得留描述":value.description;
						value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
						value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;
					})
					$scope.list = res.list;
					count = res.count;
					if(count > 10){
						$scope.showMe = true;
					}else {
						$scope.showMe = false;
					}
				}
			})
		}
	}

	$scope.page = function($event){
		if(curPage * 10 < count) {
			$event.target.innerHTML = "玩命加载中...请稍候...";	
			++curPage;
		}else {
			$event.target.innerHTML = "到底啦，到底啦，到底啦...";
			return false;
		}

		var formDataStr = $.param({
			p: curPage,
			pageSize: 10,
			school: id,
			did: $scope.dep
		})

		commonService.httpReq(getTeacherByDepartment,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				var res = response.RESULT;
				var html = "";
				$.each(res.list,function(index,value){
					value.school_name = (value.school_name=="")?"懒得留学校":value.school_name;
					value.school_name = (value.school_name.length > 10)?value.school_name.substr(0,9)+"...":value.school_name;
					value.description = (value.description=="")?"懒得留描述":value.description;
					value.description = (value.description.length > 10)?value.description.substr(0,9)+"...":value.description;
					value.pic = (value.pic == "")?default_headimg_url:urlPic + value.pic;

					html+='<li>';
					html+='	<a href="#/home/detail/'+value.id+'">';
					html+='		<div class="start_headimg">';
					html+='			<img src="'+value.pic+'">';
					html+='		</div>';
					html+='		<div class="start_name">';
					html+=			value.name;
					html+='		</div>';
					html+='		<div class="start_description">';
					html+=			value.school_name+'<br>'+value.description;
					html+='		</div>';
					html+='	</a>';
					html+='</li>';
				})

				$(".start_list > ul").append(html);
				if(curPage * 10 < res.count) {
					$event.target.innerHTML = "点击加载更多...";	
				}else {
					$event.target.innerHTML = "到底啦，到底啦，到底啦...";
				}
				
			}
		})
	}

})